﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PR.DataAccess.RamanSyrtsou;
using PR.BusinessEntity.RamanSyrtsou;

namespace PR.WebService.RamanSyrtsou
{
    class PRWebService : IPRWebServiceContractRamanSyrtsou
    {
        public List<PRObjectRamanSyrtsou> GetAllPRObjects()
        {
            using (var manager = new DataAccessManager())
            {
                List<PR_Object> objects = manager.GetAllObjects();
                return objects.Select(obj => ConvertToBE(obj)).ToList();
            }
        }

        public void DeletePRObject(int id)
        {
            using (DataAccessManager manager = new DataAccessManager())
            {
                manager.DeletePRObject(id);
            }
        }

        public int InsertPRObject(PRObjectRamanSyrtsou obj)
        {
            using (DataAccessManager manager = new DataAccessManager())
            {
                return manager.InsertPRObject(ConvertToDataAccessEntity(obj));
            }
        }

        public List<KeyValuePair<int, string>> GetAllSupportedTypes()
        {
            using (DataAccessManager manager = new DataAccessManager())
            {
                return manager.GetAllTypes()
                    .Select(type => new KeyValuePair<int, string>(type.PR_TypeID, type.Title))
                    .ToList();
            }
        }

        public List<KeyValuePair<int, string>> GetAllSupportedCategories()
        {
            using (DataAccessManager manager = new DataAccessManager())
            {
                return manager.GetAllCategories()
                    .Select(category => new KeyValuePair<int, string>(category.PR_CategoryID, category.Title))
                    .ToList();
            }
        }

        public PRObjectRamanSyrtsou GetObjectById(int id)
        {
            using (DataAccessManager manager = new DataAccessManager())
            {
                var obj = ConvertToBE(manager.GetObjectById(id));
                obj.Descriptions = manager.GetDescriptionsByObjectId(id).Select(d => ConvertToBE(d)).ToList();
                return obj;
            }
        }

        public void UpdatePRObject(PRObjectRamanSyrtsou obj)
        {
            using (DataAccessManager manager = new DataAccessManager())
            {
                manager.UpdateObject(ConvertToDataAccessEntity(obj));
            }
        }

        public PRObjectDescriptionRamanSyrtsou GetDescriptionById(int id)
        {
            using (DataAccessManager manager = new DataAccessManager())
            {
                return ConvertToBE(manager.GetDescriptionById(id));
            }
        }

        public PRObjectDescriptionRamanSyrtsou InsertDescription(PRObjectDescriptionRamanSyrtsou description)
        {
            using (DataAccessManager manager = new DataAccessManager())
            {
                int id = manager.InsertDescription(ConvertToDataAccessEntity(description));
                description.ID = id;
                return description;
            }
        }

        public void UpdateDescription(PRObjectDescriptionRamanSyrtsou description)
        {
            using (DataAccessManager manager = new DataAccessManager())
            {
                manager.UpdateDescription(ConvertToDataAccessEntity(description));
            }
        }

        public void DeleteDescriptionById(int id)
        {
            using (DataAccessManager manager = new DataAccessManager())
            {
                manager.DeleteDescription(id);
            }
        }

        private PRObjectRamanSyrtsou ConvertToBE(PR_Object row)
        {
            var be = new PRObjectRamanSyrtsou();

            be.Category = (PRObjectCategoryRamanSyrtsou)row.PR_CategoryFK;
            be.CreationDate = row.CreationDate;
            be.ID = row.PR_ObjectID;
            be.Name = row.Name;
            be.Type = (PRObjectTypeRamanSyrtsou)row.PR_TypeFK;

            return be;
        }

        private PR_Object ConvertToDataAccessEntity(PRObjectRamanSyrtsou be)
        {
            var row = new PR_Object();

            row.CreationDate = be.CreationDate;
            row.Name = be.Name;
            row.PR_CategoryFK = (int)be.Category;
            row.PR_TypeFK = (int)be.Type;
            row.PR_ObjectID = be.ID;

            return row;
        }

        private PRObjectDescriptionRamanSyrtsou ConvertToBE(PR_Description row)
        {
            var be = new PRObjectDescriptionRamanSyrtsou();

            be.ID = row.PR_DescriptionID;
            be.Text = row.Description;
            be.PRObjectFK = row.PR_ObjectFK;

            return be;
        }

        private PR_Description ConvertToDataAccessEntity(PRObjectDescriptionRamanSyrtsou be)
        {
            var row = new PR_Description();

            row.Description = be.Text;
            row.PR_DescriptionID = be.ID;
            row.PR_ObjectFK = be.PRObjectFK;

            return row;
        }
    }
}
